<template>
<div class="addTag">
  <div class="header">
    <van-nav-bar title="添加类别" left-arrow @click-left="$router.back()" />
    <van-field v-model="value" label="标签名" placeholder="请输入标签名" />
  </div>
  <div class="contain">
<!--    <van-field v-model="value" label="标签名" placeholder="请输入标签名" />-->
    <div v-for="item in ClassList" :key="item" :class=item @click="checkTagClass(item)"></div>
    <div></div>
  </div>
  <div class="okSiMiDa" @click="insertTagList">完成</div>
</div>
</template>

<script>
import {Toast} from "vant";

export default {
  name: "addTag",
  data () {
    return {
      // 标签名
      value: '',
      // 标签对应的iconfont类名
      class: '',
      // 本地的iconfont列表,可在asset/font 中进行替换
      ClassList: [
          'iconfont icon-jiaotongiconplane', 'iconfont icon-46580', 'iconfont icon-yiliao', 'iconfont icon-canyin',
          'iconfont icon-shenghuo', 'iconfont icon-canyin1', 'iconfont icon-canyin2', 'iconfont icon-shenghuojiaofei',
          'iconfont icon-shenghuo1', 'iconfont icon-shenghuo2', 'iconfont icon-shenghuojiaofei1', 'iconfont icon-shandebaoico-',
          'iconfont icon-shenghuo3', 'iconfont icon-jiaotongdanchezihangcheqihangmianxing', 'iconfont icon-yiliao1',
          'iconfont icon-jiaotong', 'iconfont icon-renqing-hongbao', 'iconfont icon-renqing', 'iconfont icon-shuiguo',
          'iconfont icon-tubiao', 'iconfont icon-lingshi', 'iconfont icon-sancanyisu',
          'iconfont icon-gongjiaoditie', 'iconfont icon-liushui'
      ]
    }
  },
  methods: {
    // 点击绑定icon的class
    checkTagClass(val) {
      this.class = val
      // console.log(val)
    },
    // 将新增的类别添加到TagList中并存储到浏览器本地
    insertTagList() {
      if (this.value === '') {
        Toast.fail('请输入标签名');
      } else if ( this.class === '' ) {
        Toast.fail('请选择标签图标');
      } else {
        // console.log('ok')
        let TagList = this.$store.state.tagList
        let exist = false
        TagList.forEach((item) =>{
          if (item.type === this.value) {
            Toast.fail('已存在相同名称的标签');
            exist = true
          }
        })
        if (!exist) {
          this.$store.commit('saveTagList',
            {
            class: this.class,
            type: this.value,
            flag: 1
          })
          // this.$store.commit('initTagList')
          Toast.success('已完成添加');
          this.$router.back()
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.addTag{
  display: flex;
  flex-flow: wrap;
  .header{
    width: 100%;
  }
  .contain{
    width: 100%;
    display: flex;
    flex-flow: wrap;
    justify-content: space-between;
    div {
      width: 20%;
      height: 7vh;
      font-size: 4vh;
      line-height: 5vh;
      text-align: center;
    }
    div:hover {
      border: #0073ff solid 2px;
    }
    div:nth-child(25):hover{
      border: #EBEFF2;
    }
  }
  // 翻译过来就是好了思密达
  .okSiMiDa {
    background-color: #1989FA;
    height: 6vh;
    text-align: center;
    line-height: 6vh;
    border-radius: 6vh;
    width: 15vh;
    margin-left: 50%;
    transform: translateX(-50%);
  }
}

</style>
